package com.example.lotterysystem.common.pojo;

import com.example.lotterysystem.common.errorcode.ErrorCode;
import com.example.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2025-06-17
 * Time: 22:24
 */
@Data
public class CommonResult<T> implements Serializable {
    /*
    返回的错误码
     */
    private Integer code;
    /*
    错误码描述
     */
    private String msg;
    /*
    正确时返回的数据
     */
    private T date;
    public static <T> CommonResult<T> success(T data){
        CommonResult<T> commonResult=new CommonResult();
        commonResult.code= GlobalErrorCodeConstants.SUCCESS.getCode();
        commonResult.date=data;
        commonResult.msg="";
        return commonResult;
    }

    public static <T> CommonResult<T> error(Integer code,String msg){
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),"code不是错误的异常");
        CommonResult<T> commonResult=new CommonResult();
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        return commonResult;
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode){
        return error(errorCode.getCode(),errorCode.getMsg());
    }
}
